
<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <meta charset="utf-8"></meta>
      <meta name="viewport" content="width=device-width, initial-scale=1.0"></meta>
      <title>14.5.&nbsp;Virtual Graph - Chapter&nbsp;14.&nbsp;Virtual Nodes &amp; Relationships (Graph Projections)</title>
      <link rel="stylesheet" type="text/css" href="../../docbook.css"></link>
      <link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"></link>
      <link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css"></link>
      <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.1.0/css/font-awesome.min.css"></link>
      <link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Open+Sans:400,300,400italic,600,300italic"></link>
      <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.11.0/codemirror.min.css"></link>
      <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.11.0/theme/neo.min.css"></link>
      <link rel="stylesheet" type="text/css" href="../../css/chunked-base.css"></link>
      <link rel="stylesheet" type="text/css" href="../../css/extra.css"></link><script src="//code.jquery.com/jquery-1.12.4.js" type="text/javascript"></script><script src="//cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js" type="text/javascript"></script><script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" type="text/javascript"></script><script src="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.11.0/codemirror.min.js" type="text/javascript"></script><script src="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.11.0/addon/runmode/runmode.min.js" type="text/javascript"></script><script src="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.11.0/mode/cypher/cypher.min.js" type="text/javascript"></script><script src="../../javascript/datatable.js" type="text/javascript"></script><script src="../../javascript/colorize.js" type="text/javascript"></script><script src="../../javascript/tabs-for-chunked.js" type="text/javascript"></script><script src="../../javascript/mp-nav.js" type="text/javascript"></script><script src="../../javascript/versionswitcher.js" type="text/javascript"></script><script src="../../javascript/version.js" type="text/javascript"></script><script src="//s3-eu-west-1.amazonaws.com/alpha.neohq.net/docs/new-manual/assets/search.js" type="text/javascript"></script><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"></meta>
      <link rel="prev" href="../virtual-nodes-rels/" title="14.2.&nbsp;Virtual Nodes/Rels"></link>
      <link rel="next" href="../graph-grouping/" title="14.6.&nbsp;Graph Grouping"></link>
      <link rel="shortcut icon" href="https://neo4j.com/wp-content/themes/neo4jweb/favicon.ico"></link><script>
        $(document).ready(function() {
          CodeMirror.colorize();
          tabTheSource($('body'));
          var $header = $('header').first();
          $header.prepend(
            $('<a href="" id="logo"><img src="https://neo4j.com/wp-content/themes/neo4jweb/assets/images/neo4j-logo-2015.png" alt="Neo4j Logo"></img></a>')
          );
          var $sidebar = $('<div id="sidebar-wrapper"></div>');
          $.get('toc.html', function (d){
            $(d).appendTo($sidebar);
            highlightToc();
            highlightLibraryHeader();
          });
          $sidebar.insertAfter($('header').first());
        });
        </script></head>
   <body>
      <header>
         <div class="searchbox">
            <form id="search-form" class="search" name="search-form" role="search"><input id="search-form-input" name="q" title="search" type="search" lang="en" placeholder="Search Neo4j docs..." aria-label="Search Neo4j documentation" max-length="128" required="required"></input><input id="search-form-button" type="submit" value="Search"></input></form>
         </div>
         <ul class="documentation-library">
            <li><a href="https://neo4j.com/docs/operations-manual/current">Operations Manual</a></li>
            <li><a href="https://neo4j.com/docs/developer-manual/current/">Developer Manual</a></li>
            <li><a href="https://neo4j.com/docs/ogm-manual/current/">OGM Manual</a></li>
            <li><a href="https://neo4j.com/docs/graph-algorithms/current/">Graph Algorithms</a></li>
            <li><a href="https://neo4j-contrib.github.io/neo4j-apoc-procedures/3.5/">APOC</a></li>
            <li><a href="https://neo4j.com/docs/java-reference/current/">Java Reference</a></li>
         </ul>
         <nav id="header-nav"><span class="nav-previous"><a accesskey="p" href="../virtual-nodes-rels/"><span class="fa fa-long-arrow-left" aria-hidden="true"></span>Virtual Nodes/Rels</a></span><span class="nav-current">
               <p class="nav-title hidden">14.5.&nbsp;Virtual Graph</p></span><span class="nav-next"><a accesskey="n" href="../graph-grouping/">Graph Grouping<span class="fa fa-long-arrow-right" aria-hidden="true"></span></a></span></nav>
      </header>
      <div id="search-results" class="hidden"></div>
      <section class="section" id="virtual-graph">
         <div class="titlepage">
            <div>
               <div>
                  <h2 class="title" style="clear: both"><a class="anchor" href="#virtual-graph"></a>14.5.&nbsp;Virtual Graph
                  </h2>
               </div>
            </div>
         </div>
         <p>Create a graph object (map) from information that&#8217;s passed in.
            It&#8217;s basic structure is: <code class="literal">{name:"Name",properties:{properties},nodes:[nodes],relationships:[relationships]}</code></p>
         <div class="informaltable">
            <div class="table" id="d0e17764">
               <table class="informaltable" border="1">
                  <colgroup>
                     <col class="col_1"></col>
                     <col class="col_2"></col>
                  </colgroup>
                  <tbody>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.graph.from(data,'name',{properties}) yield graph</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>creates a virtual graph object for later processing it tries its best to extract the graph information from the data you pass
                              in
                           </p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.graph.fromData([nodes],[relationships],'name',{properties})</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>creates a virtual graph object for later processing</p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.graph.fromPaths(path,'name',{properties})</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>creates a virtual graph object for later processing</p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.graph.fromPaths([paths],'name',{properties})</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>creates a virtual graph object for later processing</p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.graph.fromDB('name',{properties})</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>creates a virtual graph object for later processing</p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.graph.fromCypher('statement',{params},'name',{properties})</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>creates a virtual graph object for later processing</p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.agg.graph(element) as graph</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>aggregates graph elements to a "graph" map with unique sets of "nodes" and "relationships"</p>
                        </td>
                     </tr>
                  </tbody>
               </table>
            </div>
         </div>
         <p>Create a graph object (map) from information that&#8217;s passed in.
            It&#8217;s basic structure is: <code class="literal">{name:"Name",properties:{properties},nodes:[nodes],relationships:[relationships]}</code></p>
         <div class="informaltable">
            <div class="table" id="d0e17829">
               <table class="informaltable" border="1">
                  <colgroup>
                     <col class="col_1"></col>
                     <col class="col_2"></col>
                  </colgroup>
                  <tbody>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.graph.from(data,'name',{properties}) yield graph</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>creates a virtual graph object for later processing it tries its best to extract the graph information from the data you pass
                              in
                           </p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.graph.fromData([nodes],[relationships],'name',{properties})</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>creates a virtual graph object for later processing</p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.graph.fromPaths(path,'name',{properties})</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>creates a virtual graph object for later processing</p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.graph.fromPaths([paths],'name',{properties})</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>creates a virtual graph object for later processing</p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.graph.fromDB('name',{properties})</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>creates a virtual graph object for later processing</p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.graph.fromCypher('statement',{params},'name',{properties})</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>creates a virtual graph object for later processing</p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.graph.fromDocument({json},{config})</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>transform JSON documents into graph structures</p>
                        </td>
                     </tr>
                     <tr>
                        <td style="text-align: left; vertical-align: top; ">
                           <p><code class="literal">apoc.graph.validateDocument({json},{config})</code></p>
                        </td>
                        <td style="text-align: left; vertical-align: top; ">
                           <p>validate the JSON and returns informations about required fields violations</p>
                        </td>
                     </tr>
                  </tbody>
               </table>
            </div>
         </div>
         <section class="section" id="_literal_apoc_graph_fromdocument_literal">
            <div class="titlepage">
               <div>
                  <div>
                     <h3 class="title"><a class="anchor" href="#_literal_apoc_graph_fromdocument_literal"></a>14.5.1.&nbsp;<code class="literal">apoc.graph.fromDocument</code></h3>
                  </div>
               </div>
            </div>
            <p>The procedure <code class="literal">apoc.graph.fromDocument</code> transforms a JSON into a graph structure. It takes two arguments:
            </p>
            <div class="itemizedlist">
               <ul class="itemizedlist" style="list-style-type: disc; ">
                  <li class="listitem"><span class="strong"><strong>json</strong></span>, <span class="emphasis"><em>type Object</em></span>: the JSON that must be transformed. Every entry must have an <code class="literal">id</code> and a <code class="literal">type</code> (name of Label), configurable via the config params.
                     The value can be a String, or Cypher Map or List of Maps.
                  </li>
                  <li class="listitem"><span class="strong"><strong>config</strong></span>, <span class="emphasis"><em>type Map</em></span>: the configuration params
                  </li>
               </ul>
            </div>
            <p>Currently spatial and datetime properties are not handled yet.
               More advanced configuration for mapping the document is coming in future versions.
            </p>
            <p>The config is composed by the following parameters:</p>
            <div class="itemizedlist">
               <ul class="itemizedlist" style="list-style-type: disc; ">
                  <li class="listitem"><span class="strong"><strong>write</strong></span>, <span class="emphasis"><em>type boolean</em></span>: persist the graph otherwise return a Virtual Graph, default <span class="strong"><strong>false</strong></span></li>
                  <li class="listitem"><span class="strong"><strong>labelField</strong></span>, <span class="emphasis"><em>type String</em></span>: the field name that became the label of the node, default <span class="strong"><strong>type</strong></span></li>
                  <li class="listitem"><span class="strong"><strong>idField</strong></span>, <span class="emphasis"><em>type String</em></span>: the document field name that will become the id field of the created nodes (used for node resolution when you create relationships
                     between nodes), default <span class="strong"><strong>id</strong></span></li>
               </ul>
            </div><pre class="programlisting highlight"><code data-lang="json">{
    "id": 1,
    "type": "artist",
    "name": "Genesis",
    "members": ["Tony Banks", "Mike Rutherford", "Phil Collins"],
    "years": [1967, 1998, 1999, 2000, 2006]
}</code></pre><p>In this case it create one <code class="literal">Node</code> with labels <span class="strong"><strong>Artist</strong></span></p>
            <p>It also accepts list of documents:</p><pre class="programlisting highlight"><code data-lang="json">[{
    "id": 1,
    "type": "artist",
    "name": "Genesis",
    "members": ["Tony Banks", "Mike Rutherford", "Phil Collins"],
    "years": [1967, 1998, 1999, 2000, 2006]
}, {
    "id": 2,
    "type": "artist",
    "name": "Daft Punk",
    "members": ["Guy-Manuel de Homem-Christo", "Thomas Bangalter."],
    "years": [1987, 1993, 1999, 2004, 2008, 2011]
}]</code></pre><p>In this case it create 2 <code class="literal">Node</code> with labels <span class="strong"><strong>Artist</strong></span></p>
            <p>JSON Tree to graph:</p><pre class="programlisting highlight"><code data-lang="json">{
	"id": 1,
	"type": "artist",
	"name": "Genesis",
	"albums": [{
		"type": "album",
		"id": 1,
		"producer": "Jonathan King",
		"title": "From Genesis to Revelation"
	}]
}</code></pre><p>In this case it will create 2 <code class="literal">Node</code>, one <span class="strong"><strong>Artist</strong></span> and one <span class="strong"><strong>Album</strong></span> connected to each other by the <span class="strong"><strong>ALBUMS</strong></span> <code class="literal">Relationship</code></p>
         </section>
         <section class="section" id="_virtual_graph_examples">
            <div class="titlepage">
               <div>
                  <div>
                     <h3 class="title"><a class="anchor" href="#_virtual_graph_examples"></a>14.5.2.&nbsp;Virtual Graph Examples
                     </h3>
                  </div>
               </div>
            </div>
            <p>We create a dataset for our examples</p><pre class="programlisting highlight"><code data-lang="cypher">CREATE (a:Actor {name:'Tom Hanks'})-[r:ACTED_IN {roles:'Forrest'}]-&gt;(m:Movie {title:'Forrest Gump'}) RETURN *</code></pre><p><span class="formalpara-title">Virtual graph from data.&nbsp;</span>
               
            </p><pre class="programlisting highlight"><code data-lang="cypher">MATCH (n)-[r]-&gt;(m) CALL apoc.graph.fromData([n,m],[r],'test',{answer:42}) YIELD graph RETURN *</code></pre><p>
               
            </p>
            <p><span class="formalpara-title">Virtual graph from path.&nbsp;</span>
               
            </p><pre class="programlisting highlight"><code data-lang="cypher">MATCH path = (n)-[r]-&gt;(m) CALL apoc.graph.fromPath(path,'test',{answer:42}) YIELD graph RETURN *</code></pre><p>
               
            </p>
            <p><span class="formalpara-title">Virtual graph from paths.&nbsp;</span>
               
            </p><pre class="programlisting highlight"><code data-lang="cypher">MATCH path = (n)-[r]-&gt;(m) CALL apoc.graph.fromPaths([path],'test',{answer:42}) YIELD graph RETURN *</code></pre><p>
               
            </p>
            <p><span class="formalpara-title">Virtual graph from DB.&nbsp;</span>
               
            </p><pre class="programlisting highlight"><code data-lang="cypher">CALL apoc.graph.fromDB('test',{answer:42}) YIELD graph RETURN *</code></pre><p>
               
            </p>
            <p><span class="formalpara-title">Virtual graph from Cypher.&nbsp;</span>
               
            </p><pre class="programlisting highlight"><code data-lang="cypher">CALL apoc.graph.fromCypher('MATCH (n)-[r]-&gt;(m) RETURN *',null,'test',{answer:42}) YIELD graph RETURN *</code></pre><p>
               
            </p>
            <p>As a result we have a virtual graph object for later processing</p>
            <div class="informalfigure">
               <div class="mediaobject"><img src="https://raw.githubusercontent.com/neo4j-contrib/neo4j-apoc-procedures/3.4/docs/images/apoc.graph.png" width="800" alt="apoc.graph"></img></div>
            </div>
            <p><span class="formalpara-title">Virtual graph from JSON.&nbsp;</span>
               
            </p><pre class="programlisting highlight"><code data-lang="cypher">CALL apoc.graph.fromDocument("{'id': 1,'type': 'artist','name':'Genesis','members': ['Tony Banks','Mike Rutherford','Phil Collins'],'years': [1967, 1998, 1999, 2000, 2006],'albums': [{'type': 'album','id': 1,'producer': 'Jonathan King','title': 'From Genesis to Revelation'}]}", false) yield graph return graph</code></pre><p>
               
            </p>
            <p>As a result we have a virtual graph with two nodes and one relationship:</p>
            <div class="informalfigure">
               <div class="mediaobject"></div>
            </div>
         </section>
      </section>
      <footer><script type="text/javascript">
          (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
          })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
          //Allow Linker
          ga('create', 'UA-1192232-34','auto', {'allowLinker': true});
          ga('send', 'pageview');
          // Load the plugin.
          ga('require', 'linker');
          // Define which domains to autoLink.
          ga('linker:autoLink', ['neo4j.org','neo4j.com','neotechnology.com','graphdatabases.com','graphconnect.com']);
        </script><script type="text/javascript">
          document.write(unescape("%3Cscript src='//munchkin.marketo.net/munchkin.js' type='text/javascript'%3E%3C/script%3E"));
        </script><script>Munchkin.init('773-GON-065');</script></footer>
   </body>
</html>